
package jh.com.jhappbf.unload;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbDialogFragment;
import com.ab.fragment.AbDialogFragment.AbDialogOnLoadListener;
import com.ab.util.AbDialogUtil;
import com.ab.view.pullview.AbPullToRefreshView;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.ui.State;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class SaleStateQuery extends AbActivity {
    private View mNetErrorView;
    private View mDataContentView;
    private AbPullToRefreshView mRefreshView;
    private ListView mListView;
    private Button mTryAgain;
    private Context mAppContext;
    private List<JSONObject> mData;
    private List<JSONObject> mNewData;
    private MyBaseAdapter mAdapter;
    private AbDialogFragment mDialogFragment;
    private int mCurrentPage = 1;
    private int mPageSize = 30;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        mDataContentView = View.inflate(this, R.layout.dispatch_car_query_activity, null);
        mNetErrorView = View.inflate(this, R.layout.net_error_activity, null);
        setAbContentView(mDataContentView);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("订单状态查询");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        initView();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        mListView = mDataContentView.findViewById(R.id.dispatch_query_listView);
        mRefreshView = mDataContentView.findViewById(R.id.dispatch_query_pullToRefreshView);
        mTryAgain = mNetErrorView.findViewById(R.id.network_error_btn);
        mAppContext = getApplicationContext();
        mData = new ArrayList<>();
        mNewData = new ArrayList<>();
        mAdapter = new MyBaseAdapter();
        mListView.setAdapter(mAdapter);

        mTryAgain.setOnClickListener(v -> {
            setAbContentView(mDataContentView);
            loadFirst();
        });

        mRefreshView.setOnHeaderRefreshListener(view -> refreshTask());

        mRefreshView.setOnFooterLoadListener(view -> {
//				loadMore();
        });
        loadFirst();
    }

    /**
     * 第一次加载
     */
    private void loadFirst() {
        mDialogFragment = AbDialogUtil.showLoadDialog(this, R.mipmap.ic_load,
                "查询中,请稍等...");
        mDialogFragment.setCancelable(false);
        mDialogFragment.setAbDialogOnLoadListener(new AbDialogOnLoadListener() {

            @Override
            public void onLoad() {
                refreshTask();
            }
        });
    }

    /**
     * 上拉刷新
     */
    private void refreshTask() {
        mCurrentPage = 1;
        String methodName = "getSaleNoConfirm";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();

        Listener<String> listener = response -> {

            AbDialogUtil.removeDialog(SaleStateQuery.this);
            mRefreshView.onHeaderRefreshFinish();
            List<JSONObject> data = JsonHelper.getJsonObjects(response);
            if (data != null) {
                mData = data;
                if (mPageSize == data.size()) {
                    mCurrentPage++;
                } else {
                    mNewData = data;
                }
                mAdapter.notifyDataSetChanged();
            }
        };

        ErrorListener errorListener = error -> {
            AbDialogUtil.removeDialog(SaleStateQuery.this);
            mRefreshView.onHeaderRefreshFinish();
            setAbContentView(mNetErrorView);
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(getApplicationContext()).addRequest(
                webService);
    }

    /**
     * 下拉加载
     */
    private void loadMore() {
        String methodName = "unloadQuery";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("pageSizeStr");
        param.add("currentPageStr");
        param.add("user");
        value.add(String.valueOf(mPageSize));
        value.add(String.valueOf(mCurrentPage));
        value.add(State.mName);
        Listener<String> listener = response -> {

            AbDialogUtil.removeDialog(SaleStateQuery.this);
            mRefreshView.onFooterLoadFinish();
            List<JSONObject> data = JsonHelper.getJsonObjects(response);
            if (data != null) {

                if (mNewData.size() < mPageSize && mNewData.size() > 0) {
                    mData.removeAll(mNewData);
                    mNewData.clear();
                }
                if (data.size() == mPageSize) {
                    mData.addAll(data);
                    mCurrentPage++;
                } else {
                    mNewData = data;
                    mData.addAll(data);
                }
                mAdapter.notifyDataSetChanged();
            }
        };

        ErrorListener errorListener = error -> {
            AbDialogUtil.removeDialog(SaleStateQuery.this);
            mRefreshView.onFooterLoadFinish();
            setAbContentView(mNetErrorView);
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(getApplicationContext()).addRequest(
                webService);
    }

    class MyBaseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(SaleStateQuery.this,R.layout.list_view_item_txt, null);
                holder = new ViewHolder();
                holder.txt = convertView.findViewById(R.id.list_view_item_txt);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String symbol = "\r\n";
            JSONObject obj = mData.get(position);
            String builder = "车号:" +
                    obj.optString("Platenumber") +
                    symbol +
                    "货名:" +
                    obj.optString("Cargoname") +
                    symbol +
                    "毛重:" +
                    obj.optString("Gross") +
                    symbol +
                    "皮重:" +
                    obj.optString("Tare") +
                    symbol +
                    "净重:" +
                    obj.optString("Net") +
                    symbol +
                    "发货:" +
                    obj.optString("Consignor") +
                    symbol +
                    "收货:" +
                    obj.optString("Consignee") +
                    symbol +
                    "合同:" +
                    obj.optString("ContractId") +
                    symbol +
                    "类型:" +
                    obj.optString("Type") +
                    symbol +
                    "日期:" +
                    obj.optString("sDate") +
                    " " +
                    obj.optString("sTime");
            //String
            holder.txt.setText(builder);
            return convertView;
        }
    }

    class ViewHolder {
        public TextView txt;
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
